/**
 * Projeto - Listagem de projetos
 */
ProjetoLista = Ext.extend(Bt.grid.GridPanel,{
	
	//Config Options {
	
	 	 title		: 'Lista de Projetos'
		,editable	: true
		,rowEditor	: true
		,actions	: {
			visualize: false
		}
		
	//}
	
	//Initialize {
	
		,initComponent: function()
		{
			this.store = new Ext.data.DirectStore({
				 idProperty	: 'cd_projeto'
				,autoLoad	: true
				,autoDestroy: true
				,directFn	: Server.Projeto.List
				,fields	: [
					 {name: 'cd_projeto'	, type: 'int'	}
					,{name: 'cd_responsavel', type: 'int' 	}
					,{name: 'ds_descricao'	, type: 'string'}
					,{name: 'ds_versao'		, type: 'string'}
					,{name: 'ds_responsavel', type: 'string'}
					,{name: 'ds_cliente'	, type: 'string'}
					,{name: 'dt_inicio'		, type: 'date', dateFormat: 'd/m/Y'}
				]
			});
			
			Ext.apply(this,{
				tbar:[{
					 text	: 'Novo Projeto'
					,iconCls: 'ico_add'
					,handler: this._onBtnAddClick
					,scope	: this
				}]
				,columns: [{
					 header		: 'Nr.'					
					,dataIndex	: 'cd_projeto'
					,align		: 'center'
					,width		: 60
				},{
					 header		: 'Descri&#231;&#227;o'	
					,dataIndex	: 'ds_descricao'
					,width		: 200
					,editor		: {
						 xtype		: 'textfield'
						,maxLength	: 200
						,allowBlank	: false
					}		
				},{
					 header		: 'Vers&#227;o'			
					,dataIndex	: 'ds_versao'
					,width		: 100
					,editor		: {
						 xtype		: 'textfield'
						,maxLength	: 50
						,allowBlank	: false
					}		
				},{
					 header		: 'Respons&#225;vel'	
					,dataIndex	: 'cd_responsavel'
					,renderer	: function(v,m,r){ return r.data.ds_responsavel;}
					,width		: 200
					,editor		: {
						 xtype			: 'bt-combo'
						,editable		: false
						,allowBlank		: false
						,valueField		: 'cd_usuario'
						,displayField	: 'ds_nome'
						,store			: new Ext.data.DirectStore({
							 autoDestroy: true
							,directFn	: Server.Usuario.List
							,idProperty	: 'cd_usuario'	
							,fields		: [
								 {name: 'cd_usuario', type: 'int'	}
								,{name: 'ds_nome'	, type: 'string'}
							]
						})
					}	
				},{
					 header		: 'Cliente'				
					,dataIndex	: 'ds_cliente'
					,width		: 200
					,editor		: {
						 xtype		: 'textfield'
						,maxLength	: 400
						,allowBlank	: false
					}		
				},{
					 header		: 'Data de in&#237;cio'	
					,dataIndex	: 'dt_inicio'
					,align		: 'center'
					,width		: 100
					,renderer	: Ext.util.Format.dateRenderer('d/m/Y')
					,editor		: {
						 xtype		: 'datefield'
						,allowBlank	: false
					}
				}]
			});
				
			ProjetoLista.superclass.initComponent.call(this);
		}	
		
	//}
	
	//Override {
		
		,onEditClick: function(grid, record, rowIndex)
		{
			this.rowEditor.startEditing(rowIndex,false);	
		}
		
		,onRemoveClick: function(me, record, rowIndex)
		{
			Ext.Msg.confirm('Confirma&#231;&#227;o','Deseja excluir este registro?',function(opt)
			{
				if(opt === 'no'){
					return false;
				}
				 
 				Server.Projeto.Delete(record.id,function(result)
				{
					if(result && result.success)
					{
						record.store.remove(record);
					}
				});
				
			},this);
		}
		
		,onRowEditorAfterEdit: function(rowEditor, changes, record, rowIndex)
		{
			var cd_projeto = record.get('cd_projeto');
			
			this.el.setLoading(true,{text: cd_projeto ? 'Alterando projeto...' : 'Inserindo projeto...'});
			
			var objProjeto = Ext.apply({},record.data);
			objProjeto.Responsavel = {
				cd_usuario: objProjeto.cd_responsavel
			};
			delete objProjeto.cd_responsavel;
			
			Server.Projeto.Create(objProjeto);
									
			this.el.setLoading(false);
			this.store.reload();
		}
		
		,onRowEditorHide: function()
		{
			var i = this.store.findBy(function(r){ return r.phantom; });
			if(i !== -1){
				this.store.remove(this.store.getAt(i));
			}
		}
		
	//}
	
	//Listeners{
				
		,_onBtnAddClick: function()
		{
			var r = new this.store.recordType({
				 cd_projeto		: 0
				,cd_responsavel	: 0
				,ds_descricao	: ""
				,ds_versao		: ""
				,ds_responsavel	: ""
				,ds_cliente		: ""
				,dt_inicio		: ""
			},0);
			
			this.rowEditor.stopEditing();
			this.store.insert(0, r);
            this.getView().refresh();
            this.getSelectionModel().selectRow(0);
         	this.rowEditor.startEditing(0);
		}
		
	//}
	
});